﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml;

namespace Parlez.LanguageSelectors
{
    public class XMLLanguageSelector : LanguageSelector
    {
        private Dictionary<String, String> _LanguageKeys_Dictionary = new Dictionary<string,string>();

        public XMLLanguageSelector()
        {
            XmlDocument _Languages_XmlDocument = new XmlDocument();
            _Languages_XmlDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/ParlezLanguages.xml"));

            if (_Languages_XmlDocument != null)
            {
                foreach (XmlNode _Language_XmlNode in _Languages_XmlDocument.SelectNodes("/parlez/key"))
                {
                    if (_Language_XmlNode.Attributes[GetSelectedLanguage()] != null && (!_LanguageKeys_Dictionary.ContainsKey(_Language_XmlNode.Attributes["name"].Value)))
                    {
                        _LanguageKeys_Dictionary.Add(_Language_XmlNode.Attributes["name"].Value, _Language_XmlNode.Attributes[GetSelectedLanguage()].Value);
                    }
                }
            }
        }

        public override string GetLanguageItem(string Key, string Default)
        {
            if (_LanguageKeys_Dictionary.ContainsKey(Key))
            {
                return _LanguageKeys_Dictionary[Key];
            }

            return Default;
        }
    }
}
